#!/bin/bash

# 设置变量
mkdir -p output

TMPDIR="./packer/update"
DSTDIR="./packer/dist"

OUTPUT_FILE="../output/update_youdu_coremail.exe"

rm -rf $TMPDIR
mkdir -p $TMPDIR
# 更新 主程序和静态文件目录
cp ./packer/dist/start.exe $TMPDIR/start.exe
cp -r ./packer/dist/static $TMPDIR/static
cp -r ./packer/dist/templates $TMPDIR/templates
# 更新 update.sh 脚本
cp ./bash/update.sh $TMPDIR/install.sh
chmod +x $TMPDIR/install.sh
# update 目录改回 dist
rm -rf $DSTDIR
mv $TMPDIR $DSTDIR

cd packer
# 清除旧的编译文件
rm -f $OUTPUT_FILE
# 根据参数判断编译平台：w、l、la、m
if [ "$1" == "w" ]; then
    GOOS=windows GOARCH=amd64 go build -o $OUTPUT_FILE
elif [ "$1" == "l" ]; then
    GOOS=linux GOARCH=amd64 go build -o $OUTPUT_FILE
    chmod +x $OUTPUT_FILE
    # 上传到服务器
    scp $OUTPUT_FILE root@121.229.102.17:~/youdu_service/
    # 远程执行安装程序
    ssh root@121.229.102.17 "~/youdu_service/update_youdu_coremail.exe"
elif [ "$1" == "la" ]; then
    GOOS=linux GOARCH=arm64 go build -o $OUTPUT_FILE
elif [ "$1" == "m" ]; then
    GOOS=darwin GOARCH=amd64 go build -o $OUTPUT_FILE
else
    echo "Usage: $0 {w|l|la|m}"
    # echo "w: Windows 64-bit"
    # echo "l: Linux 64-bit"
    # echo "la: Linux ARM 64-bit"
    # echo "m: macOS 64-bit"
    # echo "Example: $0 w"
    # echo "Example: $0 l"
    # echo "Example: $0 la"
    # echo "Example: $0 m"
    exit 1
fi
echo "打包完成，输出文件在 $OUTPUT_FILE"

